package com.yong.sbd.biz.mybatis.controller;

import com.yong.sbd.biz.mybatis.entity.SysUser;
import com.yong.sbd.biz.mybatis.service.SysUserService;
import com.yong.sbd.common.beans.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author Kang Yong
 * @date 2025/10/24
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    /**
     * 根据ID查询用户
     *
     * @param id {@link Long}
     * @return {@link R<SysUser>}
     */
    @GetMapping("/{id}")
    public R<SysUser> getUserById(@PathVariable Long id) {
        return R.success(userService.getUserById(id));
    }

    /**
     * 根据用户名查询用户
     *
     * @param username {@link String}
     * @return {@link R<SysUser>}
     */
    @GetMapping("/username/{username}")
    public R<SysUser> getUserByUsername(@PathVariable String username) {
        return R.success(userService.getUserByUsername(username));
    }

    /**
     * 查询所有用户
     *
     * @return {@link R<List<SysUser>>}
     */
    @GetMapping
    public R<List<SysUser>> getAllUsers() {
        return R.success(userService.getAllUsers());
    }

    /**
     * 保存用户
     *
     * @param user {@link SysUser}
     * @return {@link R<Boolean>}
     */
    @PostMapping
    public R<Boolean> saveUser(@RequestBody SysUser user) {
        return R.success(userService.saveUser(user));
    }

    /**
     * 更新用户
     *
     * @param user {@link SysUser}
     * @return {@link R<Boolean>}
     */
    @PutMapping
    public R<Boolean> updateUser(@RequestBody SysUser user) {
        return R.success(userService.updateUser(user));
    }

    /**
     * 根据ID删除用户
     *
     * @param id {@link Long}
     * @return {@link R<Boolean>}
     */
    @DeleteMapping("/{id}")
    public R<Boolean> deleteUser(@PathVariable Long id) {
        return R.success(userService.deleteUser(id));
    }
}